package com.franklin.ideaplugin.easytesting.ui.history;

import com.franklin.ideaplugin.api.ui.IBaseComponent;
import com.franklin.ideaplugin.api.ui.tree.BaseSimpleNode;
import com.franklin.ideaplugin.api.utils.ProjectUtils;
import com.franklin.ideaplugin.easytesting.cache.IEasyTestingCache;
import com.franklin.ideaplugin.easytesting.entity.ExecuteResult;
import com.intellij.psi.PsiMethod;
import com.intellij.ui.components.JBLabel;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ye Junhui
 * @since 2023/6/21 23:00
 */
public class HistoryTree implements IBaseComponent {
    private JPanel panel1;
    private JTree historyTree;
    private JPanel mainPanel;

    private final PsiMethod psiMethod;

    private final ExecuteHistoryFormNew executeHistoryForm;

    public HistoryTree(PsiMethod psiMethod, ExecuteHistoryFormNew executeHistoryForm) {
        this.psiMethod = psiMethod;
        this.executeHistoryForm = executeHistoryForm;
        this.init();
    }

    private void init(){
        this.addMouseListener();
        DefaultTreeCellRenderer highLightTextTreeCellRenderer = new DefaultTreeCellRenderer(){
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                BaseSimpleNode simpleNode = (BaseSimpleNode) value;

                JBLabel jbLabel = new JBLabel();
                jbLabel.setIcon(simpleNode.getMyIcon());
                jbLabel.setText("  " + simpleNode.getName());

                return jbLabel;
            }
        };
        historyTree.setCellRenderer(highLightTextTreeCellRenderer);
        MethodNode methodNode = new MethodNode(null, psiMethod);


        IEasyTestingCache easyTestingCache = IEasyTestingCache.getInstance(ProjectUtils.getCurrProject());
        List<ExecuteResult> executeResultList = easyTestingCache.getExecuteResultList(psiMethod);
//        easyTestingCache.cacheDefaultScript(psiMethod);

        List<HistoryNode> historyNodeList = executeResultList.stream()
                .map(executeResult -> new HistoryNode(methodNode, psiMethod, executeResult))
                .collect(Collectors.toList());
        ExecuteHistoryTreeModel executeHistoryTreeModel = new ExecuteHistoryTreeModel(
                methodNode,historyNodeList
        );
        this.historyTree.setModel(executeHistoryTreeModel);
        this.historyTree.setVisible(true);
    }

    private void addMouseListener(){
        this.historyTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                TreePath pathForLocation = historyTree.getPathForLocation(e.getX(), e.getY());
                if (Objects.isNull(pathForLocation)){
                    return;
                }
                Object lastPathComponent = pathForLocation.getLastPathComponent();
                if (!(lastPathComponent instanceof HistoryNode)){
                    return;
                }
                //处理历史节点
                HistoryNode historyNode = (HistoryNode) lastPathComponent;
                executeHistoryForm.refresh(historyNode.getExecuteResult());
            }
        });
    }

    @Override
    public JComponent getMainPanel() {
        return mainPanel;
    }
}
